<template>
  <div id="app">
    <el-container class="home-container">
      <el-header>
        <div>
          <img src="../assets/head.png" alt="" />
          <span> 电商后台管理系统 </span>
        </div>
        <el-button @click="relToken">退出</el-button></el-header
      >
      <el-container>
        <el-aside :width="isCollapse ? '64px' : '200px'">
          <div class="toggle-button" @click="toggleCollapse">|||</div>
          <el-menu
            :collapse-transition="false"
            :collapse="isCollapse"
            unique-opened
            text-color="#eee"
            background-color="#333744"
            router
            :default-active="openMenu"
          >
            <!-- 一级菜单 -->
            <el-submenu
              :index="item.id + ''"
              v-for="item in menuList"
              :key="item.id"
            >
              <!-- 一级菜单模板区域 -->
              <template slot="title">
                <i :class="iocnsObj[item.id]"></i>
                <span>{{ item.authName }}</span>
              </template>
              <!-- 二级菜单 -->
              <el-menu-item
                :index="'/' + subItem.path"
                v-for="subItem in item.children"
                :key="subItem.id"
                @click="clickMenu('/' + subItem.path)"
              >
                <template slot="title">
                  <i class="el-icon-menu"></i>
                  <span>{{ subItem.authName }}</span>
                </template>
              </el-menu-item>
            </el-submenu>
          </el-menu>
        </el-aside>
        <el-main>
          <router-view></router-view>
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>

<script>
export default {
  data() {
    return {
      openMenu: '',
      menuList: [],
      iocnsObj: {
        147: 'el-icon-magic-stick',
        125: 'iconfont icon-users',
        103: 'iconfont icon-tijikongjian',
        101: 'iconfont icon-shangpin',
        102: 'iconfont icon-danju',
        145: 'iconfont icon-baobiao',
      },
      isCollapse: false,
    }
  },
  methods: {
    clickMenu(name) {
      window.sessionStorage.setItem('menu', name)
      this.openMenu = name
    },
    relToken() {
      window.sessionStorage.clear('token')
      window.sessionStorage.clear('menu')
      window.location.reload()
    },
    async getMenuList() {
      await this.$http.get('menus').then((res) => {
        let data = res.data
        if (data.meta.status !== 200) {
          return this.$message.error(data.meta.msg)
        }
        this.menuList = data.data

        console.log(data)
      })
    },
    //点击切换菜单的折叠与展开
    toggleCollapse() {
      this.isCollapse = !this.isCollapse
    },
  },
  watch:{
    $route(to){
      // console.log('去哪了',to.path)
      window.sessionStorage.setItem('menu',to.path)
      this.openMenu = to.path
    }
  },
  created() {
    this.getMenuList()
    let menu = window.sessionStorage.getItem('menu')
    if (menu) {
      this.openMenu = menu
    }
  },
}
</script>

<style lang="less" scoped>
.el-header {
  background-color: #363d40;
  color: #eee;
  text-align: center;
  display: flex;
  justify-content: space-between;
  padding-left: 0;
  align-items: center;
  font-size: 20px;
  > div {
    display: flex;
    align-items: center;
    span {
      margin-left: 15px;
    }
    img{
      width: 60px;
      height: 60px;
      border-radius: 50%;
    }
  }
}
.el-aside {
  background-color: #333744;
  color: #333;
  .el-menu {
    border-right: 0;
  }
}

.el-main {
  background-color: #e9eef3;
  color: #333;
  text-align: center;
}
.home-container {
  height: 100%;
}
.iconfont {
  margin-right: 10px;
}
.toggle-button {
  background-color: #4a5064;
  font-size: 10px;
  text-align: center;
  line-height: 24px;
  color: #fff;
  letter-spacing: 0.2em;
  cursor: pointer;
}
</style>